﻿using Authentication.Core.ServiceCommand;
using Authentication.Core.ServiceCommand.ChangeDao;
using Authentication.Model.Model;
using Authentication.Service.Core;
using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Authentication.Service
{
    public class AuthenticationService : ServiceBase, IAuthentication
    {
        public CheckLoginResponse CheckLogin(UserRecord userRecord, HttpRequestBase Request = null)
        {
            CheckLoginResponse result = new CheckLoginResponse();
            bool isSuccess = false;
            var user = Provider.Users.RetrieveUser(userRecord.UserName);
            if (user != null && !string.IsNullOrEmpty(user.UserName))
            {
                isSuccess = true;
                result.Record = new UserRecord
                {
                    GroupIdentifier = user.GroupId
                };
                if (user.GroupId.Equals("4") || user.GroupId.Equals("3"))
                {
                    if (string.IsNullOrEmpty(user.Team))
                    {
                        result.Record.DepartmentId = 1;
                    }
                    else
                    {
                        result.Record.DepartmentId = 2;
                    }
                }
            }
            else
            {
                result.ErrorMessage = string.Format("Can not found user with UserName={0}", userRecord.UserName);
                isSuccess = false;
            }
            result.IsSuccess = isSuccess;
            return result;
        }
        public RetrieveDateBusinessResponse RetrieveDateBusiness(RetrieveDateBusinessRequest request)
        {
            RetrieveDateBusinessResponse result = new RetrieveDateBusinessResponse();
            bool isSuccess = false;
            var user = Provider.Users.RetrieveBusinessDate(request.TableName);
            if (user != null && !string.IsNullOrEmpty(user.NameTable))
            {
                isSuccess = true;
                result.BusinessDate = user.DateData;
            }
            else
            {
                result.ErrorMessage = string.Format("Can not found user with UserName={0}", request.TableName);
                isSuccess = false;
                result.BusinessDate = null;
            }
            result.IsSuccess = isSuccess;
            return result;
        }
        public RetrieveCustomerCountByGroupResponse RetrieveCustomerCountByGroup(RetrieveCustomerCountByGroupRequest request)
        {
            return new RetrieveCustomerCountByGroupServiceCommand(Provider).Execute(request);
        }

        public RetrieveUserResponse RetrieveUserByUserName(RetrieveUserByUserNameRequest request)
        {
            RetrieveUserResponse response = new RetrieveUserResponse { IsSuccess = true, ErrorMessage = "" };
            try
            {
                if (request == null || request.Header == null || request.Header.ApplicationKey == null)
                {
                    response.ErrorMessage = "Invalidate the request header";
                    response.IsSuccess = false;
                    return response;
                }

                var user = Provider.Users.RetrieveUser(request.UserName);
                if (user == null)
                {
                    response.ErrorMessage = string.Format("Can not found user with ApplicationKey={0} in UserName={1}", request.Header.ApplicationKey, request.UserName);
                    response.IsSuccess = false;
                    return response;
                }
                UserRecord record = new UserRecord();
                Dictionary<string, DepartmentSme> listDepartment = new Dictionary<string, DepartmentSme>();
                if (user.GroupId.Equals("1"))
                {
                    listDepartment = Provider.Users.RetrieveListDepartment().ToDictionary(x => x.DepartmentCode);
                }
                record = new UserRecord
                {
                    UserName = user.UserName,
                    Dao = user.Dao,
                    ContractType = user.PositionName,
                    Branch = user.GroupId.Equals("1") ? listDepartment.ContainsKey(user.BranchCodeSme) ? listDepartment[user.BranchCodeSme].DepartmentName : string.Empty : user.Branch == null ? string.Empty : user.Branch.BranchNameSme,
                    StartWorkingDate = user.DateSalesStart.HasValue ? user.DateSalesStart.Value.ToShortDateString() : DateTime.Now.ToShortDateString(),
                    Zone = user.GroupId.Equals("2") ? user.BranchCodeSme : (user.Branch == null ? string.Empty : user.Branch.Region),
                    GroupIdentifier = user.GroupId,
                    GroupName = user.Group == null ? null : user.Group.GroupNameEnglish,
                    BranchCode = user.BranchCodeSme,
                    BranchId = user.Branch == null ? string.Empty : user.Branch.BranchId,
                    Email = user.Email,
                    DepartmentId = 1,
                    FullName = user.DaoName,
                    Avatar = user.Avatar,
                    Position = user.Position,
                    Team = user.Team
                };
                if (user.GroupId.Equals("1"))
                {
                    if (listDepartment.ContainsKey(user.BranchCodeSme))
                    {
                        record.DepartmentId = listDepartment[user.BranchCodeSme].DepartmentId;
                    }
                }
                else if (user.GroupId.Equals("4") || user.GroupId.Equals("3"))
                {
                    if (string.IsNullOrEmpty(user.Team))
                    {
                        record.DepartmentId = 1;
                    }
                    else
                    {
                        int total;
                        record.DepartmentId = 2;
                        if (user.Position.Equals("TeamLead"))
                        {
                            record.DaoSubOrdinatorList = Provider.Users.RetrieveListUserByDao(out total, record).Select(x => x.Dao).ToList();
                        }
                    }
                }
                response.Record = record;

            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with ApplicationKey={0} in UserName={1}", request.Header.ApplicationKey, request.UserName);
                response.IsSuccess = false;
                return response;
            }
            return response;
        }
        public RetrieveUserResponse RetrieveUserByDao(RetrieveUserByDaoRequest request)
        {
            return new RetrieveUserByDaoServiceCommand(Provider).Execute(request);
        }
        public RetrieveObjectsResponse RetrieveObjects(RetrieveObjectsRequest request)
        {
            return new RetrieveObjectsServiceCommand(Provider.Objects).Execute(request);
        }
        public RetrieveUserRolesResponse RetrieveUserRoles(RetrieveUserRolesRequest request)
        {
            return new RetrieveUserRolesServiceCommand(Provider.UserRole).Execute(request);
        }
        public RetrieveGroupRolesResponse RetrieveGroupRoles(RetrieveGroupRolesRequest request)
        {
            return new RetrieveGroupRolesServiceCommand(Provider.GroupRole).Execute(request);
        }
        public RetrieveListMsboResponse RetrieveListMsbo(RetrieveListMsboRequest request)
        {
            return new RetrieveListMsboServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord SaveActionLog(SaveActionLogRequest request)
        {
            return new SaveActionLogServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateUserQuestion(UpdateUserQuestionRequest request)
        {
            return new UpdateUserQuestionServiceCommand(Provider).Execute(request);
        }

        #region Dao Manager

        public ResponseRecord UpdateRequestDaoByGuest(UpdateCreateDaoRequestRequest request)
        {
            return new UpdateRequestDaoByGuestServiceCommand(Provider).Execute(request);
        }

        public RetrieveListRequestCreateDaoByUserResponse RetrieveListRequestCreateDaoByUser(RetrieveListRequestCreateDaoByUserRequest request)
        {
            return new RetrieveListRequestCreateDaoByUserServiceCommand(Provider).Execute(request);
        }
        public RetrieveCreateDaoRequestByIdResponse RetrieveCreateDaoRequestById(RetrieveCreateDaoRequestByIdRequest request)
        {
            return new RetrieveCreateDaoRequestByIdServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateRequestDaoByList(UpdateCreateDaoRequestRequest request)
        {
            return new UpdateRequestDaoByListServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusRequestDao(UpdateCreateDaoRequestRequest request)
        {
            return new UpdateStatusRequestDaoServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusListRequestDao(UpdateCreateDaoRequestRequest request)
        {
            return new UpdateStatusListRequestDaoServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateResultCreateDaoByList(UpdateCreateDaoRequestRequest request)
        {
            return new UpdateResultCreateDaoByListServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UploadOfferLetter(UpdateCreateDaoRequestRequest request)
        {
            return new UploadOfferLetterServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateRejectReason(UpdateCreateDaoRequestRequest request)
        {
            return new UpdateRejectReasonServiceCommand(Provider).Execute(request);
        }
        #endregion

        #region Change Dao
        public RetrieveListChangeDaoRequestResponse RetrieveListChangeDaoRequest(RetrieveListChangeDaoRequestRequest request)
        {
            return new RetrieveListChangeDaoRequestServiceCommand(Provider).Execute(request);
        }
        public RetrieveListChangeDaoRequestResponse RetrieveListChangeDaoRequestForSendMail(RetrieveListChangeDaoRequestRequest request)
        {
            return new RetrieveListChangeDaoRequestForSendMailServiceCommand(Provider).Execute(request);
        }
        public RetrieveListChangeDaoRequestResponse RetrieveListChangeDaoForExportRequest(RetrieveListChangeDaoRequestRequest request)
        {
            return new RetrieveListChangeDaoForExportRequestServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateChangeDaoRequest(UpdateChangeDaoRequestRequest request)
        {
            return new UpdateChangeDaoRequestServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateChangeDaoRequestByCifAndProduct(UpdateChangeDaoRequestRequest request)
        {
            return new UpdateChangeDaoRequestByCifAndProductServiceCommand(Provider).Execute(request);
        }
        public RetrieveChangeDaoCifResponse RetrieveChangeDaoCif(RetrieveChangeDaoCifRequest request)
        {
            return new RetrieveChangeDaoCifServiceCommand(Provider).Execute(request);
        }
        public RetrieveChangeDaoCifResponse RetrieveChangeDaoCifByListCif(RetrieveChangeDaoCifRequest request)
        {
            return new RetrieveChangeDaoCifByListCifServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateChangeDaoStatus(UpdateChangeDaoRequestRequest request)
        {
            return new UpdateChangeDaoStatusServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusListChangeDaoRequest(UpdateChangeDaoRequestRequest request)
        {
            return new UpdateStatusListChangeDaoRequestServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateRejectChangeDaoReason(UpdateChangeDaoRequestRequest request)
        {
            return new UpdateRejectChangeDaoReasonServiceCommand(Provider).Execute(request);
        }

        public ResponseRecord UpdateChangeDaoRequestMultiple(UpdateChangeDaoRequestRequest request)
        {
            return new UpdateChangeDaoRequestMultipleServiceCommand(Provider).Execute(request);
        }
        #endregion
        #region Close Dao 
        public RetrieveListCloseDaoRequestResponse RetrieveListCloseDaoRequest(RetrieveListCloseDaoRequestRequest request)
        {
            return new RetrieveListCloseDaoRequestServiceCommand(Provider).Execute(request);
        }
        public RetrieveListCloseDaoRequestResponse RetrieveListCloseDaoRequestForExport(RetrieveListCloseDaoRequestRequest request)
        {
            return new RetrieveListCloseDaoRequestForExportServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord AddListCloseDaoRequest(UpdateCloseDaoRequestRequest request)
        {
            return new AddListCloseDaoRequestServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusCloseDao(UpdateCloseDaoRequestRequest request)
        {
            return new UpdateStatusCloseDaoServiceCommand(Provider).Execute(request);
        }
        public RetrieveCloseDaoInfoResponse RetrieveCloseDaoInfoById(RetrieveCloseDaoInfoRequest request)
        {
            return new RetrieveCloseDaoInfoByIdServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusListCloseDaoRequest(UpdateCloseDaoRequestRequest request)
        {
            return new UpdateStatusListCloseDaoRequestServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UploadOfferLetterCloseDao(UpdateCloseDaoRequestRequest request)
        {
            return new UploadOfferLetterCloseDaoServiceCommand(Provider).Execute(request);
        }
        #endregion
        #region Sale code
        public RetrieveListSaleCodeResponse RetrieveListSaleCode(RetrieveListSaleCodeRequest request)
        {
            return new RetrieveListSaleCodeServiceCommand(Provider).Execute(request); 
        }
        #endregion

        #region Edit Dao 
        public RetrieveEditDaoRequestForExelResponse RetrieveListEditDaoRequestForExport(RetrieveEditDaoRequestRequest request)
        {
            return new RetrieveListEditDaoRequestForExportServiceCommand(Provider).Execute(request);
        }
        public RetrieveEditDaoRequestResponse RetrieveEditDaoRequest(RetrieveEditDaoRequestRequest request)
        {
            return new RetrieveEditDaoRequestServiceCommand(Provider).Execute(request); 
        }
        public ResponseRecord AddEditDaoRequest(UpdateEditDaoRequestRequest request)
        {
            return new AddEditDaoRequestServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusEditDao(UpdateEditDaoRequestRequest request)
        {
            return new UpdateStatusEditDaoServiceCommand(Provider).Execute(request);
        }
        public ResponseRecord UpdateStatusListEditDaoRequest(UpdateEditDaoRequestRequest request)
        {
            return new UpdateStatusListEditDaoRequestServiceCommand(Provider).Execute(request);
        }
        public RetrieveEditDaoRequestDetailResponse RetrieveEditDaoRequestDetail(RetrieveEditDaoRequestDetailRequest request)
        {
            return new RetrieveEditDaoRequestDetailServiceCommand(Provider).Execute(request);
        }
        #endregion
    }
}
